package com.hmy.uaa.controller.api;

import com.aliyuncs.auth.sts.AssumeRoleResponse;
import com.aliyuncs.exceptions.ClientException;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmy.uaa.bo.request.ReqPage;
import com.hmy.uaa.bo.request.RestetPasswordRequest;
import com.hmy.uaa.bo.request.UserRequest;
import com.hmy.uaa.bo.response.UserInfoResponse;
import com.hmy.uaa.bo.response.UserResponse;
import com.hmy.uaa.common.utils.OssStsUtil;
import com.hmy.uaa.po.User;
import com.hmy.uaa.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.groups.Default;

/**
 * 用户
 */
@Api(tags = "用户接口")
@Validated
@RestController
@RequestMapping("/api/user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;


    @Autowired
    private OssStsUtil ossStsUtil;


    @ApiOperation("用户注册")
    @PostMapping("/register")
    public void register(@RequestBody @Validated({UserRequest.Insert.class, Default.class}) UserRequest userRequest) {

        log.info(userRequest.toString());
        userService.register(userRequest);
    }

    @ApiOperation("修改用户")
    @PutMapping
    public void update(@RequestBody @Validated({UserRequest.Update.class, Default.class}) UserRequest userRequest) {

        log.info(userRequest.toString());
        userService.update(userRequest);
    }

    @ApiOperation("修改密码")
    @PutMapping("/reset-password")
    public void resetPassword(@RequestBody @Validated RestetPasswordRequest restetPasswordRequest) {

        userService.resetPassword(restetPasswordRequest);
    }

    @ApiOperation("删除用户")
    @DeleteMapping("/{id}")
    public void delete(@PathVariable Long id) {

        log.info("userId:{}", id);
        userService.delete(id);
    }

    @ApiOperation("查询用户列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "departmentId", value = "部门id"),
            @ApiImplicitParam(name = "keyWord", value = "查询关键字")
    })
    @GetMapping("/page")
    public Page<UserResponse> queryPage(ReqPage reqPage,
                                        @RequestParam(value = "departmentId", required = false) Long departmentId,
                                        @RequestParam(value = "keyWord", required = false) String keyWord) {
        Page<User> page = new Page<>(reqPage.getPageNumber(), reqPage.getPageSize());
        User user = new User();
        user.setDepartmentId(departmentId);
        user.setName(keyWord);
        user.setPhone(keyWord);

        log.info(user.toString());
        return userService.queryPage(page, user);
    }

    @ApiOperation("用户登录以后获取信息")
    @GetMapping("/info")
    public UserInfoResponse getInfo() {

        return userService.getUserInfo();
    }

    @ApiOperation("获取sts凭证")
    @GetMapping(value = "/oss-credentials")
    public AssumeRoleResponse.Credentials auth() {
        try {
            return ossStsUtil.getCredentials();
        } catch (ClientException e) {
            log.error("获取oss凭证失败", e.getErrMsg());
            throw new com.hmy.common.exception.ClientException("获取oss凭证失败");
        }
    }



}
